<template>
  <div class="login">
    <div class="left">
      <div class="top">
        <img class="login-logo" src="@/assets/login-logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登陆</span>
      </div>
      <el-form  :model="form" class="form" :rules="rules" ref="haha">
        <el-form-item prop="phone">
          <el-input prefix-icon="el-icon-user"  placeholder="请输入手机号" v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input prefix-icon="el-icon-key" placeholder="请输入验证码" v-model="form.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img class="code" :src="imgcodeSrc" alt  @click="shuaxin"/>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="anniu">
          <el-checkbox  v-model="form.anniu">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn1" type="primary" @click="tologin()">登陆</el-button>
          <br />
          <el-button class="btn2" type="primary" @click="tozhuce">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/loginr.png" alt />
      <register ref="register"/>
    </div>
  </div>
</template>

<script>
import register from '@/views/register'
import {loginCode} from '@/api/login.js'
import {baocun,quchu} from '@/utils/local.js'
export default {
  components:{
    register
  },
  created(){
    if(quchu('token')){
       this.$router.push('/layout')
    }
  },
  data() {
    return {
      imgcodeSrc:process.env.VUE_APP_URL + '/captcha?type=login',
      form: {
        phone:'',
        password:'',
        code:'',
        anniu:false,
        
      },
      rules:{
           phone:[
             {required:true,message: '请输入手机号', trigger: 'change'},
           ],
           password:[
             { required: true, message: '请输入密码', trigger: 'change' },
             { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'change' }
           ],
           code:[
              { required: true, message: '请输入验证码', trigger: 'change' },
              { min: 4, max: 4, message: '长度4', trigger: 'change' }
           ],
           anniu:[
             { validator:(rule,value,callback)=>{
                 if(value){
                   callback()
                 }else{
                   callback(new Error('请勾选'))
                 }
             }}
           ]
      }
    };
  },
  methods:{
    shuaxin(){
       this.imgcodeSrc = process.env.VUE_APP_URL + '/captcha?type=login&efwawfd='+Date.now() 
    },
    tologin(){   
        this.$refs.haha.validate(res=>{
          if(res){
            loginCode(this.form).then(res=>{
               baocun('token',res.data.data.token)
               this.$router.push('/layout')
            })
          }else{
            alert('验证失败')
          }
        })
    },
    tozhuce(){
      this.$refs.register.isshow = true
    }
  }
};
</script>

<style lang="less">
* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}
.login {
  width: 100%;
  height: 100%;
  display: flex;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background-color: #f5f5f5;
    padding: 42px;
    .top {
      display: flex;
      align-items: center;
      .login-logo {
        width: 22px;
        height: 17px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 15px;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
      .t2 {
        margin-left: 15px;
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      width: 100%;
      margin-top: 30px;
      .code {
        width: 100%;
        height: 40px;
      }
      .btn1 {
        width: 100%;
      }
      .btn2{
        width: 100%;
        margin-top: 15px;
      }
    }
  }
}
</style>